<?php

namespace addons\shopro\service\user;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\user\User as UserModel;
use app\common\model\shopro\CarQueue;
use app\common\model\shopro\UserReleaseModel;
use think\Db;

class User
{
    /**
     * @name 追加消费金额
     * @param  int|object  $user  会员对象或会员ID
     * @param  float  $amount  变更金额
     * @return boolean
     */
    public static function consume($user, $amount,$order ='')
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            error_stop('未找到用户');
        }


        // 更新会员余额信息
//        $user->setInc('total_consume', $amount);
        $performance          = $user->team_buy_money + $amount;
        //判断是否能升级
        $is_score    = false;
        if($order){
            $order_id = $order->id;
            $goodsModel            = new \app\admin\model\shopro\goods\Goods();

            foreach ($order['items'] as $k => $v) {
                $goodsInfo = $goodsModel->where(['id' => $v->goods_id])->find();
                if ($goodsInfo) {
                    if($v->goods_type == 'virtual' && $goodsInfo['car_events'] == 0){
                        $is_score = true;
                    }
                }
            }
            if($is_score){
                $accelerationModel    = new Acceleration();
                $where['performance'] = ['eq', $amount];
                $where['status']      = 1;
                $accelerationInfo     = $accelerationModel->where($where)->order('id desc')->find();
                if ($accelerationInfo) {
                    if($accelerationInfo->id > $user->level){
                        $user->level = $accelerationInfo->id;
                    }
                    $userReleaseModel = new UserReleaseModel();
                    $userReleaseInfo  = $userReleaseModel->where(['user_id'=>$user->id,'level'=>$accelerationInfo->id,'status'=>0])->find();
                    $params['id']     = $user->id;
                    $params['ext']    = ['order_id' => $order_id,'level'=>$accelerationInfo->id];
                    $params['type']   = 'score';
                    $params['amount'] = bcmul($amount,$accelerationInfo->accelerate_scale,2);
                    $params['memo']   = '购买商品赠送';
                    Db::transaction(function () use ($params,$amount) {
                        return WalletService::change($params['id'], $params['type'], $params['amount'], 'order', $params['ext'],
                            $params['memo']);
                    });

                    if($userReleaseInfo){
                        $userReleaseInfo->setInc('release',$amount);
                        $userReleaseInfo->setInc('all_release',$params['amount']);
                    }else{
                        $userReleaseModel->user_id      = $user->id;
                        $userReleaseModel->level        = $accelerationInfo->id;
                        $userReleaseModel->release      = $amount;//待释放
                        $userReleaseModel->back_release = $amount;
                        $userReleaseModel->all_release  = $params['amount'];
                        $userReleaseModel->save();
                    }
                }
            }
        }

        if($user->is_old == 1){
            $user->is_old = 0;
        }
        if($is_score){
            $user->team_buy_money = $performance;
            $user->save();
        }
        return true;
    }


    public static function twoconsume($user, $amount,$order_id=0)
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            error_stop('未找到用户');
        }


        // 更新会员余额信息
//        $user->setInc('total_consume', $amount);
        //判断是否能升级
        $accelerationModel    = new Acceleration();
        $performance          = $user->team_buy_money + $amount;
        $where['performance'] = ['eq', $amount];
        $where['status']      = 1;
        $accelerationInfo     = $accelerationModel->where($where)->order('id desc')->find();
        if ($accelerationInfo) {
            if($accelerationInfo->id > $user->level){
                $user->level = $accelerationInfo->id;
            }
            $userReleaseModel = new UserReleaseModel();
            $userReleaseInfo  = $userReleaseModel->where(['user_id'=>$user->id,'level'=>$accelerationInfo->id,'status'=>0])->find();
            $params['id']     = $user->id;
            $params['ext']    = ['order_id' => $order_id,'level'=>$accelerationInfo->id];
            $params['type']   = 'score';
            $params['amount'] = bcmul($amount,$accelerationInfo->accelerate_scale,2);
            $params['memo']   = '购买商品赠送';
            dump($amount);
//            Db::transaction(function () use ($params,$amount) {
//                return WalletService::change($params['id'], $params['type'], $params['amount'], 'order', $params['ext'],
//                    $params['memo']);
//            });
//
//            if($userReleaseInfo){
//                $userReleaseInfo->setInc('release',$amount);
//                $userReleaseInfo->setInc('all_release',$params['amount']);
//            }else{
//                $userReleaseModel->user_id      = $user->id;
//                $userReleaseModel->level        = $accelerationInfo->id;
//                $userReleaseModel->release      = $amount;//待释放
//                $userReleaseModel->back_release = $amount;
//                $userReleaseModel->all_release  = $params['amount'];
//                $userReleaseModel->save();
//            }
        }
//        if($user->is_old == 1){
//            $user->is_old = 0;
//        }
//        $user->team_buy_money = $performance;
//        $user->save();
        return true;
    }
}
